package net.lazyandroid.namewars.model;

import java.util.ArrayList;

import net.lazyandroid.namewars.utils.Log;
import net.lazyandroid.namewars.utils.StringUtils;

public class ChallengeRoundAsset 
{
	
	private static String ROUND_TOKEN = "%@%";
	private static String ROUND_ARRAY_TOKEN = "#%#";
	
	/**
	 * 
	 * @param rounds
	 * @return
	 */
	public static String encode(ArrayList<ChallengeRound> rounds)
	{
		ArrayList<String> rounds_array = new ArrayList<String>();
		
		for(int i = 0 ; i < rounds.size() ; i ++)
		{
			ChallengeRound item = rounds.get(i);
			
			ArrayList<String> round = new ArrayList<String>();
			round.add(String.valueOf(item.wait));
			round.add(String.valueOf(item.id));
			round.add(item.face);
			round.add(String.valueOf(item.hp0));
			round.add(String.valueOf(item.hp1));
			round.add(item.s);
			round.add(String.valueOf(item.wounded));
			round.add(String.valueOf(item.damage));
			rounds_array.add(StringUtils.combineByToken(round, ROUND_TOKEN));
		}
		
		String roundsStr = 
					StringUtils.combineByToken(rounds_array, ROUND_ARRAY_TOKEN);
		Log.d("ChallengeRoundAsset encode :" +roundsStr);
		
		return roundsStr ; 
		
	}
	
	/**
	 * @param roundStr
	 * @return
	 */
	public static ArrayList<ChallengeRound> decode(String roundStr)
	{
		ArrayList<ChallengeRound> rounds  = new ArrayList<ChallengeRound>();
		ArrayList<String> items = 
							StringUtils.spliteByToken(roundStr, ROUND_ARRAY_TOKEN);
		
		for(int i = 0 ; i < items.size() ; i ++)
		{
			String item = items.get(i);
			ArrayList<String> values = 
									StringUtils.spliteByToken(item,ROUND_TOKEN);
			ChallengeRound round = new ChallengeRound();
			round.wait = Integer.valueOf(values.get(0)).intValue();
			round.id = Integer.valueOf(values.get(1)).intValue();
			round.face = values.get(2);
			round.hp0 = Integer.valueOf(values.get(3)).intValue();
			round.hp1 = Integer.valueOf(values.get(4)).intValue();
			round.s   = values.get(5);
			round.wounded = Integer.valueOf(values.get(6)).intValue();
			round.damage = Integer.valueOf(values.get(7)).intValue();
			rounds.add(round);
		}
		return rounds ;
	}
}
